$(function(){
	var editor;
	KindEditor.ready(function(K) {
		editor = K.create('textarea[name="content"]', {
			uploadJson : 'http://localhost:8090/imageUpload',
			fileManagerJson : 'http://localhost:8090/fileManager',
			allowFileManager : true
		});
		K('input[name=getHtml]').click(function(e) {
			alert(editor.html());
		});
		K('input[name=isEmpty]').click(function(e) {
			alert(editor.isEmpty());
		});
		K('input[name=getText]').click(function(e) {
			alert(editor.text());
		});
		K('input[name=selectedHtml]').click(function(e) {
			alert(editor.selectedHtml());
		});
		K('input[name=setHtml]').click(function(e) {
			editor.html('<h3>Hello KindEditor</h3>');
		});
		K('input[name=setText]').click(function(e) {
			editor.text('<h3>Hello KindEditor</h3>');
		});
		K('input[name=insertHtml]').click(function(e) {
			editor.insertHtml('<strong>插入HTML</strong>');
		});
		K('input[name=appendHtml]').click(function(e) {
			editor.appendHtml('<strong>添加HTML</strong>');
		});
		K('input[name=clear]').click(function(e) {
			editor.html('');
		});

	});
	var editor2;
	KindEditor.ready(function(K) {
		editor2 = K.create('textarea[name="content2"]', {
            uploadJson : 'http://localhost:8090/imageUpload',
            fileManagerJson : 'http://localhost:8090/fileManager',
			allowFileManager : true,
			items : ["image"],
			//readonlyMode:true
		});
    });
	$("#content").val('');
	$("#content2").val('');
	$("button[name=clear]").click(function () {
		editor.html('');
		editor2.html('');
    });
	$("button[name=sub]").click(function() {
		var userId = $(".user-nav input").val();
		var content = editor.html();
		var title = $(".panel-primary").eq(0).find("textarea").val();
		var simcontent = $(".panel-primary").eq(1).find("textarea").val();
		var date = new Date();//获得时间对象
		var tag = $(".panel-primary").eq(2).find("textarea").val();
		var articleDate =  date.getFullYear()+"-"+(date.getMonth() + 1) + "-" + date.getDate();
		var articlePicture =  editor2.html().substring(11,(editor2.html()+"").length-11);
		if(content.length < 1){
			alert("正文应大于1字");
			return;
		}else if(title.length > 50 || title.length < 1){
			alert("title应在1~50字之间");
			return;
		}else if(simcontent.length > 80 || simcontent.length < 1){
			alert("文章简述应在1~80字之间");
			return;
		}else if(tag.trim() == ""){
			if ($(".panel-primary").eq(2).find("li .on").size()<=0) {
                alert("请选择至少一个标签");
                return;
            }
			else if	 ($(".panel-primary").eq(2).find("li .on").size()>2) {
					alert("一个文章最多拥有俩个标签")
					return;
			}
		} else {
            if ($(".panel-primary").eq(2).find("li .on").size()<0) {
                alert("请选择至少一个标签");
                return;
            }
            else if	 ($(".panel-primary").eq(2).find("li .on").size()>1) {
                alert("一个文章最多拥有俩个标签")
                return;
            }
		}
		addArticle(userId,content,title,simcontent,articleDate,articlePicture,tag);
		//var tag = $(".panel-primary").eq(2).find("li .on").next("input").val();
	});
	findTag();
});

function addArticle(userId,content,title,simcontent,articleDate,articlePicture,tag) {

    var a = $.ajax("addArticle", {
        data : {
            userId : userId,
            content : content,
            simcontent : simcontent,
            title : title,
            articleDate : articleDate,
            articlePicture : articlePicture,
        }
    })
    a.done(function (result) {
        if (result.code == 100) {
            addTag(userId,result.extend.article.articleId,tag);
            alert("发表成功");
            window.location.href='home';
        } else {
            alert("发表失败")
        }
    });

}

function addTag(userId,articleId,tag) {
	if (tag.trim() == ""){
		//是否有选择的标签
        $(".panel-primary").eq(2).find("li .on").each(function () {
            var tagId = $(this).next("input").val();
            var a = $.ajax("addMulti", {
                data : {
                    tagId : tagId,
                    articleId : articleId,
                    userId :userId
                }
            })
        });
	} else {
        var a = $.ajax("addTagAndMulti", {
            data : {
                tagName : tag,
                articleId : articleId
            }
        })
	}

}

function findTag() {
	var a = $.ajax({
		url:"getTags",
		type:"get"
    });
	a.done(function(result) {
		if (result.code == 100) {
			var tags = result.extend.tagList;
			$.each(tags,function(){
                var s = '<li><a href="javascript:void(0)"" ><label>';
                s += this.tagName + '</label></a><input type="hidden" value="'+this.tagId+'">'
                $('.panel-primary .panel-body ul').append(s);
			})
		}
        var flag = false;
        $('.panel-primary .panel-body li').click(function() {
            if(flag)
                $(this).find("a").addClass("on");
            else
                $(this).find("a").removeClass("on");
            flag = !flag;
        });
	});
}

function sub() {
	var file = $("#picID").val();
	if (file == "") {
		alert("请选择图片~");
	} else {
		$("#form1").submit();
	}
}